package org.agile4j.plugin.rest;

import java.util.Set;

import javax.servlet.ServletConfig;
import javax.servlet.annotation.WebServlet;

import org.agle4j.framework.helper.ClassHelper;
import org.agle4j.framework.utils.CollectionUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;

/**
 * REST Servlet
 * 
 * @author hanyx
 * @since 1.0.0
 */
@WebServlet(urlPatterns=RestConstant.SERVLET_URL, loadOnStartup=0)
public class RestServlet extends CXFNonSpringServlet{

	@Override
	protected void loadBus(ServletConfig sc) {
		// 初始化  CXF 总线
		super.loadBus(sc);
		Bus bus = getBus() ;
		BusFactory.setDefaultBus(bus);
		// 发布REST 服务
		publishRestService() ;
	}

	/**
	 * 发布REST 服务
	 */
	private void publishRestService() {
		// 遍历所有标注了 REST 注解的类
		Set<Class<?>> restClassSet = ClassHelper.getClassSetByAnnotation(Rest.class) ;
		if (CollectionUtil.isNotEmpty(restClassSet)) {
			for (Class<?> restClass : restClassSet) {
				// 获取 REST 地址
				String address = getAddress(restClass) ;
				// 发布 REST 服务
				RestHelper.publishService(address, restClass);
			}
		}
	}

	/**
	 * 获取 REST 地址
	 * @param restClass
	 */
	private String getAddress(Class<?> restClass) {
		String address ;
		// 若 REST 注解的 value 属性不为空, 则获取当前值, 否者获取类名
		String value = restClass.getAnnotation(Rest.class).value() ;
		if (StringUtils.isNotEmpty(value)) {
			address = value ;
		} else {
			address = restClass.getSimpleName() ;
		}
		return address;
	}

}
